/**
 * @file usr_low_power.c
 * @author zcj (605607217@qq.com)
 * @brief
 * @version ecu-中控
 * @date 2023-11-06
 *
 * @copyright Copyright (c) 2023
 *
 */

#include "main.h"
#include "luat_mcu.h"


/**
 * @brief 睡眠状态下轮动报警中断
 *
 * @param num
 * @return luat_pm_wakeup_pad_isr_callback_t
 * @author zcj (605607217@qq.com)
 * @date 2023-11-06
 */
void gpio_level_irq(void *data, void* args)
{
	// int pin = (int)data;    
	// LUAT_DEBUG_PRINT("pin:%d, level:%d,", pin, luat_gpio_get(pin));
   if (get_ebike_state_info(&g_ebike_status,WORK_STATUS)==V_SLEEP)//如果在低功耗
    {
        /*准备退出低功耗*/
        set_pre_quit_flag(&g_ebike_status.sleep, true);        
        /*触发轮动报警*/
        if (get_ebike_state_info(&g_ebike_status, ANTI_THEFT) == V_OFF)
        {
            set_ebike_state_info(&g_ebike_status, ANTI_THEFT, V_ON);
            luat_rtos_message_send(message_event_deal_task_handle, CASE_TASK_ON_ALARM, (void *)NULL);
        }        
    }
}
//wakeup 引脚一般作为中断引脚或者休眠唤醒引脚
void wakeup0_init(void)
{
    luat_gpio_cfg_t wakeup0_struct;
    wakeup0_struct.pin=HAL_WAKEUP_0;
    wakeup0_struct.pull=Luat_GPIO_PULLUP;
    wakeup0_struct.mode=Luat_GPIO_IRQ;
    wakeup0_struct.irq_type=LUAT_GPIO_FALLING_IRQ;
    wakeup0_struct.irq_cb=(void*)gpio_level_irq;
    luat_gpio_open(&wakeup0_struct);
}
/**
 * @brief in_count 递减
 *
 * @param sleep
 * @author zcj (605607217@qq.com)
 * @date 2023-11-06
 */
void sleep_in_count_dec(SLEEP_STATE_INFO_T *sleep)
{
    luat_rtos_mutex_lock(sleep->mutex, LUAT_WAIT_FOREVER);
    sleep->in_cnt--;
    luat_rtos_mutex_unlock(sleep->mutex);
}

/**
 * @brief in_count 递增
 *
 * @param sleep
 * @return uint8_t
 * @author zcj (605607217@qq.com)
 * @date 2023-11-06
 */
void sleep_in_count_add(SLEEP_STATE_INFO_T *sleep)
{
    luat_rtos_mutex_lock(sleep->mutex, LUAT_WAIT_FOREVER);
    sleep->in_cnt++;
    luat_rtos_mutex_unlock(sleep->mutex);
}

/**
 * @brief out_count 递减
 *
 * @param sleep
 * @return uint8_t
 * @author zcj (605607217@qq.com)
 * @date 2023-11-06
 */
void sleep_out_count_dec(SLEEP_STATE_INFO_T *sleep)
{
    luat_rtos_mutex_lock(sleep->mutex, LUAT_WAIT_FOREVER);
    sleep->out_cnt--;
    luat_rtos_mutex_unlock(sleep->mutex);
}


/**
 * @brief out_count 递增
 *
 * @param sleep
 * @return uint8_t
 * @author zcj (605607217@qq.com)
 * @date 2023-11-06
 */
void sleep_out_count_add(SLEEP_STATE_INFO_T *sleep)
{
    luat_rtos_mutex_lock(sleep->mutex, LUAT_WAIT_FOREVER);
    sleep->out_cnt++;
    luat_rtos_mutex_unlock(sleep->mutex);
}

/**
 * @brief Get the sleep in count object
 *
 * @param sleep
 * @return uint16_t
 * @author zcj (605607217@qq.com)
 * @date 2023-11-06
 */
uint16_t get_sleep_in_count(SLEEP_STATE_INFO_T *sleep)
{
    luat_rtos_mutex_lock(sleep->mutex, LUAT_WAIT_FOREVER);
    uint16_t cnt = sleep->in_cnt;
    luat_rtos_mutex_unlock(sleep->mutex);
    return cnt;
}

/**
 * @brief Get the sleep out count object
 *
 * @param sleep
 * @return uint16_t
 * @author zcj (605607217@qq.com)
 * @date 2023-11-06
 */
uint16_t get_sleep_out_count(SLEEP_STATE_INFO_T *sleep)
{
    luat_rtos_mutex_lock(sleep->mutex, LUAT_WAIT_FOREVER);
    uint16_t cnt = sleep->out_cnt;
    luat_rtos_mutex_unlock(sleep->mutex);
    return cnt;
}

/**
 * @brief 清零睡眠计数
 *
 * @param sleep
 * @author zcj (605607217@qq.com)
 * @date 2023-11-06
 */
void clear_sleep_count(SLEEP_STATE_INFO_T *sleep)
{
    luat_rtos_mutex_lock(sleep->mutex, LUAT_WAIT_FOREVER);
    sleep->out_cnt = 0;
    sleep->in_cnt = 0;
    luat_rtos_mutex_unlock(sleep->mutex);
}

void set_pre_quit_flag(SLEEP_STATE_INFO_T *sleep, bool flag)
{
    luat_rtos_mutex_lock(sleep->mutex, LUAT_WAIT_FOREVER);
    sleep->pre_quit_flag = flag;
    luat_rtos_mutex_unlock(sleep->mutex);
}

bool get_pre_quit_flag(SLEEP_STATE_INFO_T *sleep)
{
    luat_rtos_mutex_lock(sleep->mutex, LUAT_WAIT_FOREVER);
    bool flag = sleep->pre_quit_flag;
    luat_rtos_mutex_unlock(sleep->mutex);
    return flag;
}

/**
 * @brief enter low power
 *
 * @author zcj (605607217@qq.com)
 * @date 2023-11-06
 */
void usr_enter_low_power(void)
{
    usr_log(">>>>>>>>>>>>>>>>usr_enter_low_power!!!!!\r\n");
    gnss_led_ctrl(0);//灭
    net_led_ctrl(0);//灭
    luat_gpio_close(HAL_WAKEUP_PWRKEY);	//如果powerkey接地了，还需要再关闭powerkey上拉功能 91330411MA2D02243Y
    luat_pm_power_ctrl(LUAT_PM_POWER_GPS, 0);
    luat_rtos_task_sleep(1000);
    //luat_rtos_task_suspend(wheel_event_deal_task_handle);
    luat_rtos_task_suspend(gnss_led_task_handle);
    luat_rtos_task_suspend(gsm_led_task_handle);
    luat_rtos_task_suspend(gps_event_deal_task_handle);
    //luat_rtos_task_suspend(ota_event_deal_task_handle);
    //luat_rtos_task_suspend(message_event_deal_task_handle);
    luat_rtos_task_suspend(gnss_in_task_handle);
    // luat_rtos_task_suspend(ntp_init_task_handler);
    luat_rtos_task_suspend(bsloc_task_handle);
    //luat_rtos_task_suspend(publish_task_handle);
    luat_pm_power_ctrl(LUAT_PM_POWER_USB, 0);
    luat_pm_request(LUAT_PM_SLEEP_MODE_LIGHT);
    if (luat_pm_check() == LUAT_PM_SLEEP_MODE_LIGHT)
    {
        usr_log("trans to LUAT_PM_SLEEP_MODE_LIGHT");
        clear_sleep_count(&g_ebike_status.sleep);
        set_ebike_state_info(&g_ebike_status,WORK_STATUS,V_SLEEP);//进入低功耗状态
        //luat_rtos_message_send(message_event_deal_task_handle, CASE_TASK_PING, (void *)NULL);
    }
}

/**
 * @brief quit low power
 *
 * @author zcj (605607217@qq.com)
 * @date 2023-11-06
 */
void usr_quit_low_power(void)
{
    
   // luat_pm_request(LUAT_PM_SLEEP_MODE_NONE);
   luat_pm_release(LUAT_PM_SLEEP_MODE_LIGHT);
    luat_pm_power_ctrl(LUAT_PM_POWER_USB, 1);
    luat_pm_power_ctrl(LUAT_PM_POWER_GPS, 1);
    if (luat_pm_check() == LUAT_PM_SLEEP_MODE_NONE)
    {
        clear_sleep_count(&g_ebike_status.sleep);
       // task_ephemeris();
       // luat_rtos_message_send(message_event_deal_task_handle, CASE_TASK_PING, (void *)NULL);
    }    
    //luat_rtos_task_resume(wheel_event_deal_task_handle);
    luat_rtos_task_resume(gnss_led_task_handle);
    luat_rtos_task_resume(gsm_led_task_handle);
    luat_rtos_task_resume(gps_event_deal_task_handle);
   // luat_rtos_task_resume(message_event_deal_task_handle);
    luat_rtos_task_resume(gnss_in_task_handle);
    luat_rtos_task_resume(bsloc_task_handle);
    //luat_rtos_task_resume(publish_task_handle);
    work_led_mode_switch(&gsm_work_led, MODE_DATA);
    work_led_mode_switch(&gnss_work_led, MODE_DATA);
    usr_log(">>>>>>>>>>>>>>>>usr_quit_low_power!!!!!\r\n");
    usr_delay_ms(2000);
}

/**
 * @brief low power init
 *
 * @author zcj (605607217@qq.com)
 * @date 2023-11-06
 */
void low_power_init(void)
{
    usr_delay_ms(10000);
    wakeup0_init();  
    luat_pm_request(LUAT_PM_SLEEP_MODE_NONE);      
}
